<?php
	include "./include/om_config.inc";
	$smarty = new smartyWWW();
	
	$open = "";
	$close = "";
	$high = "";
	$low = "";
	$fDate = "";	
	$pivot = "";
	$R1 = "";
	$S1 = "";
	$R2 = "";
	$S2 = "";
	$R3 = "";
	$S3 = "";
	$expiryDateSelected = "";
	$expiryDateValues = "";
	$expiryDateOptions = "";
	$currentItemId = "";
	
	$itemCmb = "";
	if(isset($_POST['item']))
	  $selectedExchange = $_POST['item'];
	else
	  $selectedExchange = "";
	
	$bHigh = "";
	$bLow  = "";
	$weekDay = array();
	$i = 0;
	
	$m = 0;
  $bhavcopyCmb = "SELECT DISTINCT(contractCode) FROM bhavcopyf_o";
  $bhavcopyCmbResult = mysql_query($bhavcopyCmb); 
  while($bhavcopyRow = mysql_fetch_array($bhavcopyCmbResult))
  {
    $itemCmb[$m] = $bhavcopyRow['contractCode'];
    $m++;
  }
  if(isset($_POST['expiryDate']))
  {
  if($_POST['item']==1 || $currentItemId==$_POST['item'])
    $expiryDateSelected = 0;
  else
    $expiryDateSelected = $_POST['expiryDate'];
  }
  else
    $expiryDateSelected = 0;
    
  $z = 0;
  if(isset($_REQUEST['item']))
  {
    $currentItemId = $_REQUEST['item'];
    $expiryQuery = "SELECT DISTINCT(expiryDate),expiryDateBc FROM bhavcopyf_o WHERE  contractCode='".$currentItemId."' ORDER BY expiryDate";
    $expiryResult = mysql_query($expiryQuery);
    while($expiryRow = mysql_fetch_array($expiryResult))
    {
      $expiryDateValues[$z] = $expiryRow['expiryDate'];
      $expiryDateOptions[$z] = $expiryRow['expiryDateBc'];
      $z++;
    }
  }    
	if(isset($_REQUEST['sbmtbtn']))
	{
    $fDate   = $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-".$_REQUEST['fromDateDay'];

	  $prevMon = date("w", mktime(0,0,0, $_REQUEST['fromDateMonth'] , ($_REQUEST['fromDateDay']-1), $_REQUEST['fromDateYear']));
	  $mondat = date("Y-m-d", mktime(0,0,0, $_REQUEST['fromDateMonth'], ($_REQUEST['fromDateDay']-$prevMon)-7, $_REQUEST['fromDateYear']));
		
	  $fridat = date("Y-m-d", mktime(0,0,0, $_REQUEST['fromDateMonth'] , (($_REQUEST['fromDateDay']-$prevMon)-7)+4, $_REQUEST['fromDateYear']));
		

  
		$selectQuery = "SELECT * FROM bhavcopyf_o
            									  WHERE contractCode = '".$_REQUEST['item']."' 
            									  AND bhavcopyDate >= '".$mondat."' AND bhavcopyDate <= '".$fridat."'
            									  AND expiryDate = '".$_POST['expiryDate']."'";
		$selectQueryResult = mysql_query($selectQuery);
		$j = 0;
		while($bhavRow = mysql_fetch_array($selectQueryResult))
		{
			if($j == 0)
			  $open = $bhavRow['openPrice'];
			
			$close =  $bhavRow['closePrice'];
//			$msg = '<tr bgColor=lime><td colspan=4 align=center><font color=red><b>This Is Valid Data </b></font></td></tr>';
			$j++;
		}

		$selectQueryMax = "SELECT MAX(highPrice) as highPrice FROM bhavcopyf_o
										   WHERE contractCode = '".$_REQUEST['item']."' 
										   AND expiryDate    = '".$_REQUEST['expiryDate']."'
										   AND bhavcopyDate >= '".$mondat."' AND bhavcopyDate <= '".$fridat."'";
		$selectQueryMaxResult = mysql_query($selectQueryMax);
		if($selectQueryMaxRow = mysql_fetch_array($selectQueryMaxResult))
		  $high = $selectQueryMaxRow['highPrice'];

		$selectQueryMin = "SELECT MIN(lowPrice) as lowPrice FROM bhavcopyf_o
										   WHERE contractCode = '".$_REQUEST['item']."' 
										   AND expiryDate   = '".$_REQUEST['expiryDate']."'
										   AND bhavcopyDate >= '".$mondat."' AND bhavcopyDate <= '".$fridat."'";
		$selectQueryMinResult = mysql_query($selectQueryMin);
		if($selectQueryMinRow = mysql_fetch_array($selectQueryMinResult))
		  $low = $selectQueryMinRow['lowPrice'];

	  $pivot = round(($high + $low + $close)/3,2);

	  $R1 = (2 * $pivot) - $low;
	  $S1 = (2 * $pivot) - $high; 

	  $R2 = $pivot + $R1 - $S1;
	  $S2 = $pivot - $R1 + $S1; 

	  $R3 = $high + 2*($pivot - $low); 
	  $S3 = $low  - 2*($high  - $pivot);
	  
	  $bHigh = round((($high * 2) + $low)/3,2);
	  $bLow  = round((($low  * 2)  + $high)/3,2);
	  
    $i++;
	}

//	$smarty->assign("msg",$msg);
	$smarty->assign("open",$open);
	$smarty->assign("close",$close);
	$smarty->assign("high",$high);
	$smarty->assign("low",$low);
	$smarty->assign("fDate",$fDate);
	$smarty->assign("pivot",$pivot);
	$smarty->assign("R1",$R1);
	$smarty->assign("R2",$R2);
	$smarty->assign("R3",$R3);
	$smarty->assign("S1",$S1);
	$smarty->assign("S2",$S2);
	$smarty->assign("S3",$S3);
	$smarty->assign("bHigh",$bHigh);
	$smarty->assign("bLow",$bLow);
	$smarty->assign("itemCmb",$itemCmb);
	$smarty->assign("expiryDateSelected",$expiryDateSelected);
  $smarty->assign("expiryDateValues",$expiryDateValues);
  $smarty->assign("expiryDateOptions",$expiryDateOptions);

	$smarty->assign("selectedExchange",$selectedExchange);

	$smarty->display("report.tpl");
?>